<script type="text/javascript">
    function sync2WPress() {
        var $ = jQuery;
        $('#cyan-export .status').empty();
        $('#cyan-export').addClass('cyan-exporting');
        var image = "<?php echo plugin_dir_url(__FILE__) . 'progressBar.gif'; ?>";
        $('#cyan-export .status').html('正在同步 <img src="' + image + '" align="middle"/>');
        $.ajax({
                url : ajaxurl,
                data : {
                    'action': 'changyan_sync2Wordpress'
                },
                timeout : 60000,
                success : function (response) {
                    if ((response.search(/失败|error|fail|false/i)) >= 0) {
                        if ((response.search(/空值/i)) >= 0) {
                            alert("同步失败：APP ID错误，请检查您的输入");
                        } else {
                            alert("同步失败：" + response);
                        }
                        $('#cyan-export .message-complete').html('同步失败');
                    }
                },
                error : function(XMLHttpRequest, error, status){
                    alert(error);
                    if(error != null && 0 == XMLHttpRequest.status){
                        alert("连接失败，请检查您的网络设置");
                    } else {
                        alert("同步失败，HTTP错误代码：" + XMLHttpRequest.readyState);
                    }
                    $('#cyan-export .message-complete').html('同步失败');
                },
                complete : function(){
                    $('#cyan-export').removeClass('cyan-exporting').addClass('cyan-exported');
                }
        });
    }

    function sync2Cyan(flag) {
        var $ = jQuery;
        $('#cyan-WP2cyan .status').empty();
        $('#cyan-WP2cyan').addClass('cyan-exporting');
        if (($('#appID').val().length != 9) || ($('#appKey').val().length != 32)) {
            alert("请输入正确的APP ID和APP KEY");
            window.location.reload();
            return;
        }
        var image = "<?php echo plugin_dir_url(__FILE__) . 'progressBar.gif'; ?>";
        $('#cyan-WP2cyan .status').html('正在同步 <img src="' + image + '" align="middle"/>');
        $.ajax({
                url : ajaxurl, 
                data : {
                    'action': 'changyan_sync2Changyan'
                },
                timeout : 60000,
                success : function (response) {
                    if ((response.search(/失败|error|fail|false/i)) >= 0) {
                        if ((response.search(/站点不存在/i)) >= 0) {
                            alert("同步失败：APP ID错误，请检查您的输入");
                        } else {
                            alert("同步失败：" + response);
                        }
                        $('#cyan-WP2cyan .message-complete').html('同步失败');
                    } else {
                        if ("T" == flag) {
                            alert("同步成功，开始享受畅言带来的方便吧");
                            window.location.reload();
                        } else {
                            $('#cyan-WP2cyan .message-complete').html('同步完成');
                        }
                    }
                },
                error : function(XMLHttpRequest, error, status){
                    alert(error);
                    if(error != null && 0 == XMLHttpRequest.status){
                        alert("连接失败，请检查您的网络设置");
                    } else {
                        alert("同步失败，HTTP错误代码：" + XMLHttpRequest.readyState);
                    }
                    $('#cyan-WP2cyan .message-complete').html('同步失败');
                },
                complete : function(){
                    $('#cyan-WP2cyan').removeClass('cyan-exporting').addClass('cyan-exported');
                }
        });
    } 

    function saveAppID() {
        var $ = jQuery;
        var appID = $('#appID').val();
        if (appID.length != 9) {
            return -1;
        }
        var data = {
            action: 'changyan_saveAppID',
            appID: appID
        };
        jQuery.post(ajaxurl, data, function (response) {
            $('#appID').val(response);
        });
        return 0;
    }

    function saveAppKey() {
        var $ = jQuery;
        var appKey = $('#appKey').val();
        if (appKey.length != 32) {
            return -1;
        }
        var data = {
            action: 'changyan_saveAppKey',
            appKey: appKey
        };
        $.post(ajaxurl, data, function (response) {
            $('#appKey').val(response);
        });
        return 0;
    }

	function saveAppIDKey() {
        var $ = jQuery;

        var appID = $('#appID').val();
        if (appID.length != 9) {
            return -1;
        }

        var appKey = $('#appKey').val();
        if (appKey.length != 32) {
            return -1;
        }
        var data = {
            action: 'changyan_saveAppIDKey',
            appID: appID,
            appKey: appKey
        };
        $.post(ajaxurl, data, function (response) {});
        return 0;
	}

$(document).ready(function(){

    $('#changyanCron').click(function () {
        var $ = jQuery;
        var isChecked = !!($(this).attr('checked'));
        var data = {
            action: 'changyan_setCron',
            isChecked: isChecked
        };
        $.post(
                ajaxurl,
                data,
                function (response) {
                    if (response == 'TRUE') {
                        //alert("提交成功！");
                    } else {
                        alert("提交异常，请检查您的网络设置");
                    }
                }
        );
    });
	
	
    $(function () {
        var $ = jQuery;
        var iframe = $('#rightBar_1');
        var resetIframeHeight = function () {
            iframe.height($(window).height() - iframe.offset().top - 70);
        };
        resetIframeHeight();
        $(window).resize(resetIframeHeight);
    });
})

    function saveAppKey_AppID() {
        var $ = jQuery;
        var $appButton = $('#appButton');
        var buttonName = $appButton.val();
        if (buttonName == "提交") {
            if (-1 == saveAppIDKey()) {
                alert("请输入正确的APP ID和APP KEY");
                return;
            }
            $('#appID').attr('disabled', true);
            $('#appID').addClass('inputbox-disable');
            $('#appKey').attr('disabled', true);
            $('#appKey').addClass('inputbox-disable');
            $appButton.removeClass('button-primary').val("修改");
        } else {
            $('#appID').attr('disabled', false);
            $('#appID').removeClass('inputbox-disable');
            $('#appKey').attr('disabled', false);
            $('#appKey').removeClass('inputbox-disable');
            $appButton.addClass('button-primary').val("提交");
        }
    }

</script>
